<!--
	This is the header of text.php
-->
<?php 
include 'ecstore_v3_config.php';
include ($site_root . '/insp-modules/misc/config.php');?>
<?php
	$conn = mysql_connect($host, $user, $password) or die(mysql_error());
	mysql_select_db("travel");
	$sql = "SELECT * FROM address";
	$result = mysql_query($sql, $conn) or die(mysql_error());
?>

<?php 
echo <<< HERE
<html class="no-js" lang="en">

<head>
   <meta charset="utf-8" />

   <title>Quick Mart</title>


   <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;" />
   <meta name="format-detection" content="telephone=no" />

   <!-- todo <link rel="shortcut icon" type="image/ico" href="images/favicon.ico" />-->

    <link rel="stylesheet" href="css/reset.css" />
	<link rel="stylesheet" href="css/utils.css" />
	<link rel="stylesheet" href="css/layout.css" />
	<link rel="stylesheet" href="css/typography.css" />
	<link rel="stylesheet" href="css/lists.css" />
	<link rel="stylesheet" href="css/forms.css" />
	<link rel="stylesheet" href="css/blocks.css" />
	<link rel="stylesheet" href="css/media-queries.css" />

	<script src="js/json2.js"></script>
	<script src="js/zepto.js"></script>
	<script src="js/m-trapeze.js"></script>
	<script src="js/m-carousel.js"></script>
	<script src="js/m-statusbar.js"></script>

   
   
   
   
   <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://code.google.com/apis/gears/gears_init.js"></script> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true&libraries=geometry"></script>
<script type="text/javascript"> 
 
var initialLocation;
var browserSupportFlag =  new Boolean();
var map;
  
var myLoc = new Array();
  var message = new Array();
  var info = new Array();
HERE;

// getting all address information from database
	$n = 0;
	while ($row = mysql_fetch_assoc($result)) {
echo <<< HERE

		info[$n] = new Array();
		info[$n][0] = "$row[id]";
		info[$n][1] = "$row[address]";
		info[$n][2] = "$row[city]";
		info[$n][3] = "$row[state]";
		info[$n][4] = "$row[zipcode]";
		info[$n][5] = "$row[phone]";
		info[$n][8] = "$row[weekday]";
		info[$n][9] = "$row[sat]";
		info[$n][10] = "$row[sun]";
		
HERE;
		echo "myLoc[$n] = new google.maps.LatLng($row[lat],$row[lng]);"; 
		$n++;
	}

$loc = $_REQUEST["location"];	
echo <<< HERE

var address = "$loc";
var geocoder;
var lat_temp;
var lng_temp;

HERE;

// initialize() is to get the result of the nearest 3 travel agencies near your current location or the place you input by zipcode or city name.
echo <<< HERE
function initialize() {
	
  // Try W3C Geolocation method (Preferred)
  if(navigator.geolocation) {
    browserSupportFlag = true;
    navigator.geolocation.getCurrentPosition(function(position) {
		if (address == "") {
			initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
			var distance = new Array();
			var temp = new Array();
		
			for (var i = 0; i < $n; i++) {
				distance[i] = google.maps.geometry.spherical.computeDistanceBetween(myLoc[i], initialLocation);
				distance[i] = distance[i]/1000*1.54;
				distance[i] = distance[i].toFixed(1);
				temp[i] = distance[i];
				message[i] = distance[i].toString() + " mi";
			}
		
			temp.sort(function(a,b){return a-b;});
			var alertMessage = new Array();
			
			var cont_h = document.getElementById("Content");
			var l_h = "<header class=\"row\"><h2>Locations close to your current location</h2></header>";
			var oDiv_h = document.createElement('div');
			oDiv_h.innerHTML = l_h;
			cont_h.appendChild(oDiv_h);
			
			for (var j = 0; j < 4; j++) {
				for (var k = 0; k < $n; k++) {
					if (temp[j]==distance[k]) {
						var msg = info[k][1] + ", " + info[k][2] + ", " + info[k][3];
						var cont = document.getElementById("Content");
						var l;
						l = "<div class=\"row\"><a href=\"#\" id=\"" + k + "\" onclick=\"show(this.id)\">" + info[k][2] + ",  " + info[k][3] 
							+ "</a><br/>"+ info[k][1] + ", " + info[k][2] + ", " + info[k][3] + ", " + info[k][4] + "<br/>" 
							+ "<a href=\"tel:" + info[k][5] + "\" target=\"_self\">" + info[k][5] + "</a>" + "<br/>" + message[k] + "<br/>"
							+ "<ul class=\"buttons\"><li style=\"float:left\"><a href=\"tel:" + info[k][5] + "\" target=\"_self\" class=\"button-secondary\">Call</a></li>"
							+ "<li style=\"float:left;margin-left:10px\"><a href=\"current.php?startlat=" + initialLocation.lat() + "&startlng=" + initialLocation.lng() 
							+ "&endlat=" + myLoc[k].lat() + "&endlng=" + myLoc[k].lng() 
							+ "&addr=" + msg 
							+ "\" class=\"button-secondary\">Map</a></li>"
							+ "<li style=\"float:left;margin-left:10px\"><a href=\"direction.php?startlat=" + initialLocation.lat() + "&startlng=" + initialLocation.lng()
							+ "&endlat=" + myLoc[k].lat() + "&endlng=" + myLoc[k].lng()
							+ "&addr=" + msg
							+ "\" class=\"button-secondary\">Direction</a></li>"
							+ "</ul><br/></div>";
						var oDiv = document.createElement('div');
						oDiv.innerHTML = l;
						cont.appendChild(oDiv);
					}
				}
			}
		} else {
			geocoder = new google.maps.Geocoder();
			geocoder.geocode( { 'address': address}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					
					lat = results[0].geometry.location.lat();
					lng = results[0].geometry.location.lng(); 
					initialLocation = new google.maps.LatLng(lat, lng);
					var distance = new Array();
					var temp = new Array();
		
					for (var i = 0; i < $n; i++) {
						distance[i] = google.maps.geometry.spherical.computeDistanceBetween(myLoc[i], initialLocation);
						distance[i] = distance[i]/(1000*1.61);
						distance[i] = distance[i].toFixed(1);
						temp[i] = distance[i];
						message[i] = distance[i].toString() + " mi";
			
					}
		
					temp.sort(function(a,b){return a-b;});
					var alertMessage = new Array();
		
					var cont_h = document.getElementById("Content");
					var l_h = "<header class=\"row\"><h2>Locations close to \"" + address + "\"</h2></header>";
					var oDiv_h = document.createElement('div');
					oDiv_h.innerHTML = l_h;
					cont_h.appendChild(oDiv_h);
					
					for (var j = 0; j < 4; j++) {
						for (var k = 0; k < $n; k++) {
							if (temp[j]==distance[k]) {
								var cont = document.getElementById("Content");
								var l;
								l = "<div class=\"row\"><a href=\"#\" id=\"" + k + "\" onclick=\"show(this.id)\">" + info[k][2] + ",  " + info[k][3] 
									+ "</a><br/>"+ info[k][1] + ", " + info[k][2] + ", " + info[k][3] + ", " + info[k][4] + "<br/>" 
									+ "<a href=\"tel:" + info[k][5] + "\" target=\"_self\">" + info[k][5] + "</a>" + "<br/>" + message[k] + "<br/>"
									+ "<ul class=\"buttons\"><li style=\"float:left\"><a href=\"tel:" + info[k][5] + "\" target=\"_self\" class=\"button-secondary\">Call</a></li>"
									+ "<li style=\"float:left;margin-left;margin-left:10px\"><a href=\"current.php?startlat=" + initialLocation.lat() + "&startlng=" + initialLocation.lng() 
									+ "&endlat=" + myLoc[k].lat() + "&endlng=" + myLoc[k].lng() 
									+ "&addr=" + info[k][2] 
									+ "\" class=\"button-secondary\">Map</a></li>"
									+ "<li style=\"float:left;margin-left:10px\"><a href=\"direction.php?startlat=" + initialLocation.lat() + "&startlng=" + initialLocation.lng()
									+ "&endlat=" + myLoc[k].lat() + "&endlng=" + myLoc[k].lng()
									+ "&addr=" + info[k][2]
									+ "\" class=\"button-secondary\">Direction</a></li>"
									+ "</ul><br/></div>";
								var oDiv = document.createElement('div');
								oDiv.innerHTML = l;
								cont.appendChild(oDiv);
							}
						}
					}	
				} else {
					alert("Geocode was not successful for the following reason: " + status);
				}
			});
		}
		
    }, function() {
      handleNoGeolocation(browserSupportFlag);
    });
  } else if (google.gears) {
    // Try Google Gears Geolocation
    browserSupportFlag = true;
    var geo = google.gears.factory.create('beta.geolocation');
    geo.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
    }, function() {
      handleNoGeolocation(browserSupportFlag);
    });
  } else {
    // Browser doesn't support Geolocation
    browserSupportFlag = false;
    handleNoGeolocation(browserSupportFlag);
  }
  
}
HERE;

// shows the alert of whole detail
echo <<< HERE
function show(k) {
	alert("Address: " + info[k][1] + ", " + info[k][2] + ", " + info[k][3] + ", " + info[k][4] + "\\nPhone: " + info[k][5] + "\\nOffice Hour:\\n"
		+ "Mon - Fri : " + info[k][8] + "\\nSat :            " + info[k][9] + "\\nSun :           " + info[k][10]);
	/*var light = document.getElementById("light");
	var ele;
	var c = "";
	c += "<div><p>Address: " + info[k][1] + ", " + info[k][2] + ", " + info[k][3] + ", " + info[k][4] + "</p>";
	c += "<p>Phone: <a href=\"tel:(732)881-2713\" target=\"_self\">(732)881-2713</a></p></div>";
	ele = document.createElement('div');
	ele.innerHTML = c;
	light.appendChild(ele);
	light.style.display='block';*/
	
}

function handleNoGeolocation(errorFlag) {
  if (errorFlag == true) {
    initialLocation = newyork;
    contentString = "Error: The Geolocation service failed.";
  } else {
    initialLocation = siberia;
    contentString = "Error: Your browser doesn't support geolocation. Are you in Siberia?";
  }
  map.setCenter(initialLocation);
  infowindow.setContent(contentString);
  infowindow.setPosition(initialLocation);
  infowindow.open(map);
}
</script> 
   


</head>

<body class="home-page" onload="initialize()">

   <div id="Wrapper">
       <header id="Chrome">
           <h1 id="Logo"></h1>
           <nav id="ChromeNav">
               <ul>
                   <li id="NavHome"><a href="/travel/home.php" class="icon">Home</a></li>
                   <li id="NavLocator"><a href="/locator/search/">
					<span class="word">Find</span> <span class="word">Locations</span></a></li>
                   <li id="NavSearch"><a href="/search/" class="icon">Search</a></li>
               </ul>
           </nav>

           <form id="FormLocator" action="choice.php" method="get" class="chrome-block">
               <div class="container">
                   <label for="id_location">Enter Zip or City Name, State</label>
                   <div class="wrapper">
                       <button id="LocatorCurrent" class="button-primary">Use Curent Location</button>
                       <input id="id_location" name="location" type="text"  />
                       <input id="id_lat" name="lat" type="hidden" />
                       <input id="id_lng" name="lng" type="hidden" />
                       <input id="id_geocode_results" name="geocode_results" type="hidden" />
                       <button id="LocatorSearch" class="button-secondary" type="submit">Search</button>
                   </div>
               </div>
           </form>

           <form id="FormSearch" action="/travel/sphider/search.php" method="get" class="chrome-block">
               <div class="container">
                   <label for="id_q">Search</label>
                   <div class="wrapper">
                       <input id="query" name="query"  type="text" />

                       <input type="hidden" name="c" id="id_c" />

                       <button class="button-secondary" type="submit">Search</button>
                   </div>
               </div>
           </form>
       </header>
	   
HERE;
?>